//
// Created by lenovo on 2024/8/11.
//

#ifndef TREELIB_TREE_MULTITREE_TEMPLATENODE_CLASS
#define TREELIB_TREE_MULTITREE_TEMPLATENODE_CLASS

#include "./__multitree__.h"
#include <vector>
using namespace std ;
namespace TREE{
    template <typename T>
    class TemplateNode : public Node<T>{
    public :
        T _data ;
        vector<Node<T>*> _nodes ;
    public:
        Node<T>*        getChild    (int n) override;
        Node<T>*        parent      () override ;
        void            pop         (int n) override ;
//        void          setChild    (T data , int n) override ;
        virtual bool    _acceptable (Node<T>* node) ;   //setChild的辅助函数,用以增加灵活性
        void            setChild    (Node<T>* node , int n) override ;
        int             width       ()  const override ;
        int           capacity    ()  const override ;
        T               data        () override ;

        virtual ~TemplateNode() ;
    };

    template<typename T>
    int TemplateNode<T>::capacity() const {
        return INT_MAX;
    }

    template<typename T>
    Node<T> *TemplateNode<T>::parent() {
        return nullptr;
    }

    template<typename T>
    bool TemplateNode<T>::_acceptable(Node<T> *node) {
        return true;
    }

    template<typename T>
    T TemplateNode<T>::data() {
        return _data;
    }

    template<typename T>
    int TemplateNode<T>::width() const {
        return _nodes.size();
    }

    template<typename T>
    void TemplateNode<T>::setChild(Node<T> *node, int n) {
        if (not _acceptable(node))
            return ;

        if (n >= width()){
            for (size_t i = width() ; i <= n ; i ++){
                _nodes.push_back(nullptr);
            }
        }
        _nodes[n] = node ;
    }

    template<typename T>
    void TemplateNode<T>::pop(int n){
        if (n >= width())
            return ;

        if (n < width()) {
            delete _nodes[n];
            _nodes.erase(_nodes.begin() + n);
        }
    }

    template<typename T>
    Node<T> *TemplateNode<T>::getChild(int i) {
        return _nodes[i];
    }

    template <typename T>
    TemplateNode<T>::~TemplateNode() {
        //释放所有nodes
        for (auto node : _nodes) {
            delete node;
        }
        while (not _nodes.empty()){
            _nodes.pop_back();
        }
    }
}



#endif //TREELIB_TREE_MULTITREE_TEMPLATENODE_CLASS
